//需要处理脚本多次加载的问题，是否在不必要的时候不加载
(function(window) {
    var ct = {
        win: {},
        fn: [],
        //functions array
        user: {} //登录信息
    };
    ct.initData = {};
    ct.init = function() {
        this.user.guid = this.win.win.initData.guid;
        $('*[advanced-title]').title();
        this._parseLocation();
        console.log(this.win);
        ///这里为什么不起作用
    }
    ///异步输出脚本文件
    ct.asyncScript = function(file) {
        document.write("<script type='text/javascript' src='/js/" + file + "'></script>");
    }
    ///同步输出脚本文件
    ct.loadjson = function(file) {
        return $.sync(file, function($rst) {
            return $rst;
        }, 'json');
    }
    ct._parseLocation = function() {
        loc = this.win.location;
        _data = /^(http[s]?):\/\/([\w\.]+):?(\d*)(\/?[\w\/\.]+)\?m=([\w\-\/]*\/)?(\w+)\.?(\w*)$/.exec(loc);
        if (_data) {
            this.win.protocal = _data[1];
            this.win.port = _data[3] == "" ? 80 : _data[3];
            this.win.host = _data[2];
            this.win.subdir = _data[4];
            this.win.ctl.dir = _data[5];
            this.win.ctl.ctl = _data[6];
            this.win.ctl.action = (_data[7] == '' ? 'index' : _data[7]);
            this.win.ctlstr = this.win.ctl.dir + this.win.ctl.ctl + '.' + this.win.ctl.action;

            this.win.root = this.win.protocal + '://' + this.win.host + (this.win.port == 80 ? '' : ':' + this.win.port) + this.win.subdir;
            this.win.home = this.win.protocal + '://' + this.win.host + (this.win.port == 80 ? '' : ':' + this.win.port);
        }
    }
    //console.log($(window).width);
    //框架中打开窗口、iframe都是子窗口，创建的模拟窗口暂时未入窗口列表

    ct.win = {
        win: window,
        width: $(window).width(),
        height: $(window).height(),
        name: Math.random(),
        children: {},
        parent: this,
        location: window.location.href.toString(),
        host: '',
        //主机地址
        root: '',
        //完整的相对目录跟地址
        home: '',
        //主页地址
        subdir: '/',
        //如果站点存在子目录
        protocal: 'http',
        port: '80',
        gets: [],
        ctl: {
            'dir': '',
            'ctl': '',
            'action': ''
        },
        //控制器(路径，类，方法)
        ctlstr: '',
        //控制器z字符串表示方法
        open: function(url, name, feature) {
            if (!name) name = Math.random();
            children[name] = window.open(url, name, feature);
        }
    };
    ct.user.isLogin = function() {
        return !!this.guid;
    };
    ct.alert = function(title, content, cover_bg, auto_move, with_closed, width, height, animated_in, animated_out, custom_class, custom_cover_class) {
        //caller;
        _btn_html = '<div class="prompt selected">OK</div>';
        this.box(title, content, cover_bg, auto_move, with_closed, width, height, animated_in, animated_out, custom_class, custom_cover_class, _btn_html);
    };

    ct.confirm = function(title, content, cover_bg, auto_move, with_closed, width, height, animated_in, animated_out, custom_class, custom_cover_class) {
        _btn_html = '<div class="prompt selected">Yes</div><div class="prompt">No</div>';
        this.box(title, content, cover_bg, auto_move, with_closed, width, height, animated_in, animated_out, custom_class, custom_cover_class, _btn_html);
    };

    ct.prompt = function(title, msg, cover_bg, auto_move, with_closed, width, height, animated_in, animated_out, custom_class, custom_cover_class) {
        _btn_html = '<div class="prompt selected">OK</div><div class="prompt">Cancel</div>';
        //content=$('<div style="padding:10px;" />').append($('<p>'+msg+'</p>')).append($('<textarea style="width:90%;"/>')).html();
        content = '<p class="msg">' + msg + '</p>' + '<textarea style="resize:none;margin-left:2%;width:95%;height:60%"></textarea>';
        this.box(title, content, cover_bg, auto_move, with_closed, width, height, animated_in, animated_out, custom_class, custom_cover_class, _btn_html);
    };
    //ct.ani_type={'fade':1};
    ///dom jqdom url html
    //custom_class 自定义样式时的包裹类
    ct.box = function(title, content, cover_bg, auto_move, with_closed, width, height, animated_in, animated_out, custom_class, custom_cover_class, btn_html) {
        if (cover_bg) {
            _cover = $('<div class="ct-cover' + (custom_cover_class == undefined ? '' : ' ' + custom_cover_class) + '"/>');
            $('body').append(_cover);
        }
        _movehander_class = 'handler';
        _warp = $('<div class="ct-box' + (custom_class == undefined ? '' : ' ' + custom_class) + '"/>');

        _mover = $('<div class="' + _movehander_class + '"/>');
        _mover.append($('<h3>' + title + '</h3>'));
        _warp.append(_mover);
        if (with_closed) {
            _closehander_class = 'closer';
            _closer = $('<div class="' + _closehander_class + '"><b>×</b></div>');
            _mover.append(_closer);
            _closer.click(function() {
                if (_cover) _cover.remove();
                ///动画处理
                if (animated_out) {
                    _warp.animate(animated_out, 500, function() {
                        _warp.remove();
                    });
                } else
                _warp.remove();

            });
        }
        if (width != undefined) _warp.width(width);
        if (height != undefined) _warp.height(height);

        _inside = $('<div class="box-inside"></div>');

        _inside.append(content);

        _warp.append(_inside);
        _btn = undefined;
        if (btn_html != undefined) {
            _btn = $('<div class="box-btn-area">' + btn_html + '</div>');
            _warp.append(_btn);
        }
        _warp.hide();
        $('body').append(_warp);

        _warp.css({
            'left': (this.win.width - _warp.outerWidth()) / 2,
            'top': (this.win.height - _warp.outerHeight()) / 2
        });

        _inside.height(_warp.height() - _mover.outerHeight() - (_btn == undefined ? 0 : _btn.outerHeight()));
        _warp.autopos({'left':'auto','top':'auto'});
        if (auto_move) {
            _warp.automove(_movehander_class);
        }
        if (animated_in) {
            __width = _warp.width();
            __height = _warp.height();
            __left = _warp.css('left');
            __top = _warp.css('top');
            __opacity = _warp.css('opacity');
            //_warp.fadeIn('slow');
            _warp.css(animated_in);
            _warp.show();
            _warp.animate({
                'width': __width,
                'height': __height,
                'left': __left,
                'top': __top,
                'opacity': __opacity
            }, 500, function() {

            });
        } else {
            _warp.show();
        }
        if (content.selector == '#loadiframe') {
            _load = $('#load-img');
            _inside.append(_load);
            //该图片的加载也需要时序，在加载iframe前保证图片已经被加载了 使用sync操作完成同步请求
            ///这边左偏居需要考究
            _load.css({
                'margin-left': '45%',
                'margin-top': (_load.parent().innerHeight() - _load.height()) / 2 + 'px'
            });
            _load.show();
        }

    }
    ///专用的控制器方法集合类
    ct.ctl = {
        ///是否在控制器数组中，重点这里需要匹配一些默认的情况 比如index action
        inCtlArray:function(ctl,arr){
            //对ctl进行加index处理 而后匹配是否是后面的字串
            return in_array(ctl,arr);
        }

    };

    ct.net = {
        //复杂的客户端和服务器同步操作
        timeCorrect: function() {},

        connect: function($url) {
            _date1_unix = _date2_unix = _date3_unix = 0;
            _date1 = new Date();
            _date1_unix = _date1.getTime();

            if (!$url) $url = 'http://ctphp.local.com/app/direct/net_test.php' + '?_rand=' + Math.random();

            _date2_unix = parseFloat($.sync($url, function(rst) {
                return rst;
            }), 'text', 5 * 1000);
            // check the ok with the date
            if (_dat2_unix != NaN) {
                _date3 = new Date();
                _date3_unix = _date3.getTime();
                rst = {
                    'up': _date2_unix - _date1_unix / 1000,
                    'down': _date3_unix / 1000 - _date2_unix,
                    'all': _date2_unix - _date1_unix / 1000
                };
                if (rst.up < 0) rst.up = -2; //problem with the timenape
                if (rst.down < 0) rst.down = -2; //problem with the timenape
                return rst;
            } else {
                return {
                    'up': -1,
                    'down': -1,
                    'all': -1
                };
            }
        }

    };

    window.ct = ct;
    //ct.init();
})(window);

//console.log(window.ct);